#pragma once
#include <vector>
#include <iostream>
#include <fstream>
#include <numeric>
#include <algorithm>
#include <cmath>
#include "tools.h"
#include "wavreader.h"

// Perfect combination: 10 7
#define MELS_NUM 20
#define STFT_NUM 10
#define DATA_NUM 1000
#define DEBUG false

extern bool is_train;
extern bool is_test_accuracy;

class wavAudio
{
private:
	// wave's header
	unsigned int data_length;
	int format, channels, sample_rate, bits_per_sample, samples_num;

	// wave file's data
	std::vector<int16_t> data, process_data;

	// max_data is max value in Spectrogram, and _per is the 10 percent of the max value
	int max_data = INT_MIN, _per = 0;
	int begin_clock = 0, end_clock = 0;
public:
	// character
	float duration = 0;
	int pitch = 0;
	int average_loudness = 0;
	int energy = 0;

	float mels_mean = 0;
	float mels_variance = 0;
	float mels_min = 0;
	float mels_max = 0;

	float sftf_real_mean = 0;
	float sftf_real_variance = 0;
	float sftf_real_max = 0;
	float sftf_real_min = 0;
	float sftf_imag_mean = 0;
	float sftf_imag_variance = 0;
	float sftf_imag_max = 0;
	float sftf_imag_min = 0;

	std::vector<float> melsArray;
	std::vector<float> sftfRealArray;
	std::vector<float> sftfImagArray;
	
	wavAudio() {
		this->data_length = 1;
		this->format = 1;
		this->channels = 1;
		this->sample_rate = 1;
		this->bits_per_sample = 16;
		this->samples_num = 1;
		this->data = std::vector<int16_t>(this->samples_num, 0);
	}
	wavAudio(const char* filepath) {
		this->readInfo(filepath);
	}
	void readInfo(const char* filepath); // read wave file information

	void processdata(); // pre-processing

	void calculatePitch(); // calculate the pitch

	void calculateAverageLoudness(); // calculate the average loudness

	void calculateTime(); // calculate the continue time

	void calculateEnergy(); // calculate the energy contained in audio

	void calculateSTFT();

	void calculateMelspectrogram();

	void computeCharacter(); // compute character

	std::vector<float> returnCharacter();

	~wavAudio();
};

